import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from "@angular/router";
import { MyHttp } from "../../../../service/MyHttp";
import { HMPipe } from "../../../../pipe/hm.pipe";

@Component({
  selector: 'app-orderdetail',
  templateUrl: './orderdetail.component.html',
  styleUrls: ['./orderdetail.component.css']
})
export class OrderdetailComponent implements OnInit {
  public recordType = ['新租', '正常续租', '超时续租']
  private orderId: string = '';
  private doorType = ['小', '中', '大'];
  public data: any;
  constructor(private myHttp: MyHttp, private activeRoute: ActivatedRoute) {
    this.activeRoute.params.subscribe(params => {
      if (params['orderId']) {
        this.orderId = params['orderId'];
        this.getOrderDetail();
      }
    })
  }

  ngOnInit() {
  }

  getOrderDetail() {
    this.myHttp.post('order/findOne', { "order_number": this.orderId }).subscribe(res => {
      if (res.errorCode == 0) {
        res.message.door_size = this.doorType[res.message.door_size];
        //分割成数组  
        let str = res.message.created_at.replace(/-/g, '/');     
        let datums = new Date(new Date(str).getTime() + (res.message.order_status==2?res.message.records[0].use_time:res.message.order_use_time) *60*1000);
        res.message.order_end_time = this.formatDate(datums);
        this.data = res.message;
      }
    }, err => { alert('获取失败') })
  }



  private formatDate(now):string {
    let year = now.getFullYear();
    let month = this.formatValue(now.getMonth() + 1);
    let date = this.formatValue(now.getDate());
    let hour = this.formatValue(now.getHours());
    let minute = this.formatValue(now.getMinutes());
    let second = this.formatValue(now.getSeconds());
    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
  }
  private formatValue(num):string{
    let str = num.toString();
    return str.length>1?str:('0'+str);
  }
}
